package com.shuoyue.adven.main.bussiness;

import androidx.viewpager.widget.ViewPager;

import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import com.danikula.videocache.HttpProxyCacheServer;
import com.dueeeke.videoplayer.player.VideoView;
import com.shuoyue.adven.Constant;
import com.shuoyue.adven.R;
import com.shuoyue.adven.cache.ProxyVideoCacheManager;
import com.shuoyue.adven.entity.Adv;
import com.shuoyue.adven.entity.ImpotentResult;
import com.sm.appbase.base.basemvp.BaseMvpActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ImpotentAdvActivity extends BaseMvpActivity {
    Handler handlerScroll;
    Runnable scrolltask;
    protected List<Adv> mVideos = new ArrayList<>();
    public PropertyAdapter mGalleryAdapter;
    @BindView(R.id.imgs_pager)
    public ViewPager imgsPager;
    @BindView(R.id.videoView)
    public VideoView mVideoView;
    ImpotentResult impotentResult;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
        initView();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_impotent_adv;
    }

    protected void initView() {
        mVideoView.setEnableAudioFocus(false);
        mVideoView.setOnStateChangeListener(new VideoView.SimpleOnStateChangeListener() {
            @Override
            public void onPlayStateChanged(int playState) {
                Log.e("SubVideoState", playState + "");
                //监听VideoViewManager释放，重置状态
                if (playState == VideoView.STATE_IDLE) {
                    mVideoView.release();
                } else if (playState == VideoView.STATE_ERROR || playState == VideoView.STATE_PLAYBACK_COMPLETED) {
                    startScroll();
                    mVideoView.release();
                    mVideoView.setVisibility(View.INVISIBLE);
                }
            }
        });
        mGalleryAdapter = new PropertyAdapter(mContext,mVideos);
        imgsPager.setAdapter(mGalleryAdapter);
        handlerScroll = new Handler();
        scrolltask = new Runnable() {
            @Override
            public void run() {
                handlerScroll.postDelayed(this, Constant.IMPOTENT_SCROLL_DELAY);
                imgsPager.setCurrentItem(imgsPager.getCurrentItem() + 1);
            }
        };

        mGalleryAdapter.setImgScaleType(ImageView.ScaleType.CENTER_INSIDE);
        imgsPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (mVideos.size() == 0)
                    return;
                Adv Adv = mVideos.get(position % mVideos.size());
                if (Adv.getType() == Adv.TYPE_VIDEO) {
                    startPlay(position);
                } else {
                    mVideoView.release();
                }
                if (position > mVideos.size() && impotentResult.getAdPositionInfo().getShowModel() == 2) {
                    finish();
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
//        setVideoList(getTestScreen2());
        startScroll();
    }

    @Override
    public void initData() {
        impotentResult = (ImpotentResult) getIntent().getSerializableExtra("data");
        mGalleryAdapter.resetData(impotentResult.getList());
    }

    /**
     * 开始播放
     *
     * @param position 列表位置
     */
    protected void startPlay(int position) {
        stopScroll();
        Log.e("method", "startPlay");
        mVideoView.setVisibility(View.VISIBLE);
        Adv videoBean = mVideos.get(position % mVideos.size());
        HttpProxyCacheServer cacheServer = ProxyVideoCacheManager.getProxy(mContext);
        String proxyUrl = cacheServer.getProxyUrl(videoBean.getVideoUrl());
        mVideoView.setUrl(proxyUrl);
        mVideoView.setUrl(proxyUrl);
        mVideoView.release();
        mVideoView.start();

    }

    @Override
    protected void onStop() {
        super.onStop();
        if (mVideoView != null) {
            mVideoView.release();
        }
    }

    @Override
    public void onBackPressed() {
        if (mVideoView == null || !mVideoView.onBackPressed()) {
            super.onBackPressed();
        }
    }


    void startScroll() {
        handlerScroll.postDelayed(scrolltask, Constant.IMPOTENT_SCROLL_DELAY);
    }

    void stopScroll() {
        handlerScroll.removeCallbacks(scrolltask);
    }


    //<editor-folder desc="开放给Activity的方法">
    public void setVideoList(List<Adv> videoList) {
        mVideos.clear();
        mVideos.addAll(videoList);
        if (mGalleryAdapter != null)
            mGalleryAdapter.notifyDataSetChanged();
    }
    //</editor-folder>
}
